package com.application.Activity;
import java.util.ArrayList;

import com.application.Activity.R;
import com.application.Controller.ActivityController;
import com.application.Controller.ReadWriteFileController;
import com.application.DataProvider.PartDAO;

import ReadWriteXML.CTestofPart;
import ReadWriteXML.ReadXML;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;


public class FrmListTest extends ListActivity{

	ArrayList<CTestofPart> _listTest = new ArrayList<CTestofPart>();
	ArrayAdapter<String> _arrAdapterList;
	ArrayList<String> _listTitle = new ArrayList<String>();
	ArrayList<ArrayList<Integer>> _index = new ArrayList<ArrayList<Integer>>();
	String _fName = null;
	private static int _idPart;
	private static int _idDetailLearning;
	TextView _tvTitle;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.frm_listtest);
		_tvTitle = (TextView)findViewById(R.id.frmlisttest_tvTitleBanner);
		
		//show tên part
		PartDAO pDao = new PartDAO(null);
		String namePart = pDao.getName(_idPart);
		_tvTitle.setText(_tvTitle.getText() +"chủ đề "+ namePart);
		showList();
	}
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		ArrayList<Integer> pos= _index.get(position);
		_fName = _listTest.get(pos.get(0)).get_listNameTest().get(pos.get(1));
		ClickItemTestList();
	}
	private void ClickItemTestList()
	{
		String path = ReadWriteFileController.DIRECTORYTESTCONTENT+_fName + ".xml";
		Log.d("PEERDROID","path:  " + path);
		int status = ReadXML.getIntValue(path, "Status");
		FrmDoTest.set_status(status);
		if(status == 1)
		{
			FrmDoTest.set_fNameQues(_fName);
			ActivityController.getPeerActivity().startActivity(new Intent(ActivityController.getPeerActivity().getBaseContext(),FrmDoTest.class));
		}
		if(status == 2)
		{
			FrmShowResultOfTest.set_fNameQues(_fName);
			FrmShowResultOfTest.set_status(2);
			ActivityController.getPeerActivity().startActivity(new Intent(ActivityController.getPeerActivity().getBaseContext(),FrmShowResultOfTest.class));
		}
		if(status == 3)
		{
			FrmShowResultOfTest.set_fNameQues(_fName);
			FrmShowResultOfTest.set_status(3);
			ActivityController.getPeerActivity().startActivity(new Intent(ActivityController.getPeerActivity().getBaseContext(),FrmShowResultOfTest.class));
		}
	}
	private void showList(){
		String path = ReadWriteFileController.DIRECTORYTEST + "testList.xml";
		_listTest = ReadXML.ReadTestList(path, _idPart, _idDetailLearning);
		if(_listTest.isEmpty())
		{

		}
		int i = 0;
		for(CTestofPart test : _listTest)
		{
			addListTitle(test, i);
			i++;
		}
		_arrAdapterList=new ArrayAdapter<String>(this,R.layout.row_listtest,R.id.txtrowlisttest,_listTitle);
		this.setListAdapter(_arrAdapterList);
	}	

	private void addListTitle(CTestofPart test, int i)
	{
		int n = test.get_titleNameTest().size();
		for(int j = 0; j < n; j++)
		{
			ArrayList<Integer> temp = new ArrayList<Integer>();
			temp.add(i);
			temp.add(j);
			_index.add(temp);
			_listTitle.add(test.get_titleNameTest().get(j));
		}
	}
	@Override
	public void onPause(){
		super.onPause();
	//	ActivityController.setPeerActivity(this);
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//		ShowSubjectList();	
		Log.d("PEERDROID", "vao resume");
		//ActivityController.setPeerActivity(this);
	}
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.d("PEERDROID","vao onstart list test");
	//	ActivityController.setPeerActivity(this);
	}
	public static void set_idPart(int _idPart) {
		FrmListTest._idPart = _idPart;
	}
	public static int get_idPart() {
		return _idPart;
	}
	public static void set_idDetailLearning(int _idDetailLearning) {
		FrmListTest._idDetailLearning = _idDetailLearning;
	}
	public static int get_idDetailLearning() {
		return _idDetailLearning;
	}

}
